<?php
include_once('common/header.php');			
if($AppUI->id <=0 || !isset($_SERVER['HTTP_REFERER'])) {
	Vfriend_Function::redirect(HOST_VFRIEND);
}
$intType		= $objRequest->getParam('type', 0);
$intOfflineId	= $objRequest->getParam('offlineid', 0);
$intOjoinId		= $objRequest->getParam('ojoinid', 0);
$intOwnerId		= Vfriend_Function::base64D($objRequest->getParam('ow', ''));
$intUserId		= Vfriend_Function::base64D($objRequest->getParam('uid', ''));
$strToken		= $objRequest->getParam('token', '');
$strConstSess	= ($intType == 1 || $intType == 3) ? SE_OFFLINE_ACCEPT : SE_OFFLINE_DENY;
$strIdSess		= $intType < 3 ? $intOfflineId : $intOjoinId;
$strSession		= Vfriend_Function::getSecret($strIdSess, $strConstSess);
$strRefer		= isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$intError		= 0;
if(Vfriend_Function::isValidLink($strRefer) && $strToken != '' && $strToken == $strSession) {
	Vfriend_Function::deleteSecret($strIdSess, $strConstSess);
	$objOffline = new Offline();
	$arrRow		= $objOffline->get($intOfflineId);
	
	$arrOjoin	= $objOffline->getOjoin($AppUI->id,$intOfflineId);
	if(isset($arrRow) && (
				($intType < 3 && $arrRow['userid'] == $intOwnerId && isset($arrOjoin) && $arrOjoin['statuses'] == -1)
					||
				($intType > 2 && $arrRow['userid'] == $AppUI->id)
			)
		){
		if(isset($arrRow['expired']) && $arrRow['expired'] != '0000-00-00 00:00:00' && ( $intType == 1 || $intType == 3 )) {
			$intTime 	= time();
			$intTimeExp	= strtotime($arrRow['expired']);
			if($intTime > $intTimeExp) $intError = 26;
		}
		if($intError == 0) {
			if($intType < 3) {
				$intStatus	= ($intType == 1) ? 1 : -2;
				$intError	= $objOffline->ojoinUpdateStatus($AppUI->id, $arrOjoin['ojoinid'], $intOfflineId, $intOwnerId, $intStatus);
				if($intError == 0) {
					if($intType == 1) Vfriend_Function::setMsg($arrMessage[9]);
					else Vfriend_Function::setMsg($arrMessage[10]);
				} else {
					$intError += 24;
				}
			}elseif($intType > 2) {				
				$intStatus	= ($intType == 3) ? 1 : 2;
				$intError	= $objOffline->ojoinUpdateStatus($intUserId, $intOjoinId, $intOfflineId, $AppUI->id, $intStatus, 0);
				if($intError == 0) {				
					if($intType == 3) $strMessage = $arrMessage[11];
					else $strMessage = $arrMessage[12];
					$objVfriendUser	 = new Vfriend_User();
					$objUser		 = $objVfriendUser->getFull($intUserId);
					$strMessage		 = str_replace('[strFullName]', $objUser->fullnamevn, $strMessage);
					Vfriend_Function::setMsg($strMessage);
				} else {
					if($intError == 3) {
						$intError = 28;
					} else {
						$intError += 24;
					}
				}
			}
		}
	} else {
		$intError = 25;
	}
} else {
	$intError	= 25;
}
if($intError > 0) {	
	Vfriend_Function::setError($arrError[$intError]);	
}
$strRefer	= $strRefer ? $strRefer : LINK_OFFLINE;
Vfriend_Function::redirect($strRefer);
?>